<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


use App\Http\Requests;
use DB;
class ArticleController extends Controller
{
	 /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $arts = DB::table('news')->orderBy('id','title')->paginate(5);
        return view('art.index',['arts'=>$arts]);

        /*//删除数据
        $res = DB::table('arts')->where('id','>',1)->delete();
        dd($res);
        //修改
        $res = DB::table('arts')->where('id',2)->update(['content'=>'update2']);
        dd($res);
        //插入数据
        $art = ['title'=>'2','content'=>'2'];
        $res = DB::table('arts')->insert($art);
        dd($res);
        //查询数据
        $arts = DB::table('arts')->get();
        dd($arts);
        return view('art.index',['title'=>$title]);*/


         //$cats = DB::table('cats')->first();

/*
        $res = DB::table('goods')
            ->leftJoin('cats','goods.cat_id','=','cats.cat_id')
            ->select('goods.goods_name','cats.cat_name')
            ->where('cats.cat_id','=',11)
            ->get();
        dd($res);
        //聚合函数
        //$count = DB::table('goods')->count();
        $max = DB::table('goods')->max('shop_price');

        $res = DB::table('goods')
            ->groupBy('cat_id')
            ->having('cat_id','>',8)
            ->select('cat_id',DB::raw('count(*) as number'))
            ->where('cats.cat_id','=',11)
            ->get();
        dd($res);*/


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
	}
    public function create()
    {
    	return view('art.add');
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
    	$data = $request->except('_token');
    	$res = DB::table('news')->insert($data);
    	if($res){
    		return redirect('/art');
    	}else{
    		return back();
    	}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    	$art = DB::table('news')->where('id','=',$id)->first();
    	return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    	$art = DB::table('news')->where('id','=',$id)->first();
    	return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    	$res = DB::table('news')->where('id','=',$id)
    		->update(['title'=>$request->title,'keywords'=>$request->keywords]);
    	return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
    	$res = DB::table('news')->where('id',$id)->delete();
    	return redirect('/art');
    }

}
